<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        const arr = [
            { 'id': '29', 'pid': '', 'name': '总裁办' },
            { 'id': '2c', 'pid': '', 'name': '总裁办' },
            { 'id': '2d', 'pid': '2c', 'name': '马仔1' },
            { 'id': '2t', 'pid': '2c', 'name': '马仔2' },
            { 'id': 'd2', 'pid': '', 'name': '总裁办' },
            { 'id': 'd3', 'pid': '2t', 'name': '猪逼' },

        ]
        const map = {}
        arr.forEach(item => {
            if (!item.children) {
                item.children = []
            }
            map[item.id] = item  //调用堆地址中的数据
        })
        console.log(arr);  //添加children
        console.log(map); //给每一个item添加一个属性名id

        arr.forEach(item => {
            const father = map[item.pid]
            console.log(father, '爹'); //undefine

            if (father) {
                father.children.push(item)
                console.log(father, '爹1');
            } else {

            }

        })

    </script>
</body>

</html>